package com.xinjie.www.servlet;

import com.xinjie.www.po.User;
import com.xinjie.www.service.user.UserServiceImpl;
import com.xinjie.www.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("1".equals(method)) {
            //更新用户名
            this.updateUsername(req, resp);
        }
        if ("2".equals(method)) {
            //更新密码
            this.updatePassword(req, resp);
        }
        if ("3".equals(method)) {
            //更新个性签名
            this.updateSign(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    //更新用户名
    private void updateUsername(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        User user = (User) o;
        String newUsername = req.getParameter("newUsername");
        System.out.println(newUsername);
        if (o != null && newUsername != null && !"".equals(newUsername)) {
            System.out.println("进入修改用户名");
            UserServiceImpl userService = new UserServiceImpl();
            boolean flag = userService.updateUsername(user.getUserId(), newUsername, user.getUserName());
            if (flag) {
                req.getSession().setAttribute("ModifyUserNameSuccess", "用户名修改成功，请重新登录");
                req.getSession().removeAttribute(Constants.USER_SESSION);
                resp.sendRedirect("/login.jsp");
            } else {
                System.out.println("用户名修改失败");
                req.getSession().setAttribute("ModifyUserNameLose", "用户名修改失败");
                resp.sendRedirect("/UserPage/Modify.jsp");
            }
        }else {
            System.out.println("用户名修改失败");
            req.getSession().setAttribute("ModifyUserNameLose", "用户名修改失败");
            resp.sendRedirect("/UserPage/Modify.jsp");
        }
    }

    //更新密码
    private void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newPassword1 = req.getParameter("newPassword1");
        String newPassword2 = req.getParameter("newPassword2");
        System.out.println(newPassword1);
        System.out.println(newPassword2);
        boolean flag = false;
        if (o != null && newPassword1 != null && !"".equals(newPassword1) && newPassword2 != null && !"".equals(newPassword2)) {
            System.out.println("进入修改密码");
            UserServiceImpl userService = new UserServiceImpl();
            if (newPassword1.equals(newPassword2)) {
                flag = userService.updatePassword(((User) o).getUserId(), newPassword2);
            } else {
                req.getSession().setAttribute("Noequal", "前后密码不一致,请重新输入");
                resp.sendRedirect("/UserPage/ModifyPassword.jsp");
            }
            if (flag) {
                req.getSession().setAttribute("ModifyPasswordSuccess", "密码修改成功，请重新登录");
                req.getSession().removeAttribute(Constants.USER_SESSION);
                resp.sendRedirect("/login.jsp");
            } else {
                req.getSession().setAttribute("ModifyPasswordLose", "密码修改失败");
                resp.sendRedirect("/UserPage/Modify.jsp");
            }
        }else {
            req.getSession().setAttribute("ModifyPasswordLose", "密码修改失败");
            resp.sendRedirect("/UserPage/Modify.jsp");
        }
    }

    //更新个性签名
    private void updateSign(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newSign = req.getParameter("newSign");
        System.out.println(newSign);
        boolean flag = false;
        if (o != null && newSign != null && !"".equals(newSign)) {
            System.out.println("进入修改个性签名");
            UserServiceImpl userService = new UserServiceImpl();
            flag = userService.updateSign(((User) o).getUserId(), newSign);
            if (!flag) {
                req.getSession().setAttribute("ModifySignLose", "个性签名修改失败");
                resp.sendRedirect("/UserPage/ModifySign.jsp");
            } else {
                req.getSession().setAttribute("ModifySignSuccess", "个性签名修改成功");
                resp.sendRedirect("/UserPage/Modify.jsp");
            }
        }else {
            req.getSession().setAttribute("ModifySignLose", "个性签名修改失败");
            resp.sendRedirect("/UserPage/ModifySign.jsp");
        }
    }

}
